package com.fc.advbox.controller.api;

import com.fc.advbox.common.ResponseJson;
import com.fc.advbox.controller.admin.BaseController;
import com.fc.advbox.entity.EquipmentMaintainEntity;
import com.fc.advbox.service.IEquipmentMaintainService;
import com.fc.advbox.utils.Constant;
import com.fc.advbox.utils.RRException;
import com.fc.advbox.utils.validator.ValidatorUtils;
import com.fc.advbox.utils.validator.group.AddGroup;
import com.fc.advbox.utils.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by E7450 on 2018/6/21.
 */
@RestController
@RequestMapping("/api/equipment/maintain")
@Api(value="设备维修接口", tags={"设备维修接口"})
public class EquipmentMaintainController extends BaseController {

    @Autowired
    private IEquipmentMaintainService equipmentMaintainService;

    @ApiOperation(value = "POST -- 查询设备维修记录",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>可选参数: equipmentId: 设备Id</i></li>" +
                    "<li><i>可选参数: code: 设备编码 模糊查询</i></li>" +
                    "<li><i>可选参数: name: 设备名称，模糊查询</i></li>" +
                    "<li><i>可选参数: type: 设备类型，例如： WF-01, WF-02, WF-03, WF-04 </i></li>" +
                    "<li><i>可选参数: city: 地区, 例如：'Suzhou'</i></li>" +
                    "<li><i>可选参数: district: 区域， 模糊查询</i></li>" +
                    "<li><i>可选参数: tenantId: 承租人ID</i></li>" +
                    "<li><i>默认参数：page: 1, 当前页，默认值1</i></li>" +
                    "<li><i>默认参数：limit: 每次查询个数，默认为10</i></li>" +
                    "</ul>")
    @PostMapping("/list")
    @RequiresPermissions("api:equipment:view")
    public ResponseJson doQueryList(@RequestBody Map<String, Object> params) {
        //校对并赋值 offset和limit
        calculatePagination(params);

        List<EquipmentMaintainEntity> maintainList = equipmentMaintainService.queryList(params);
        return ResponseJson.success(maintainList);
    }


    @ApiOperation(value = "GET -- 查看单个设备维修信息",
            notes = " REST 方式请求" +
                    "/one/{maintainId}",
            response = ResponseJson.class,
            httpMethod = "GET",
            consumes = "application/json",
            produces = "application/json",
            code = 200
    )
    @GetMapping("/one/{maintainId}")
    @RequiresPermissions("api:equipment:view")
    public ResponseJson doQueryInfo(@PathVariable("maintainId") Long maintainId) {

        if(maintainId == null || maintainId == 0)
        {
            throw new RRException("ID不能为空!");
        }

        EquipmentMaintainEntity maintainEntity = equipmentMaintainService.queryObject(maintainId);
        return ResponseJson.success(maintainEntity);
    }

    @ApiOperation(value = "Put -- 更新设备信息",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>必填：id: 设备维修实体主键</i></li>" +
                    "<li><i>选填：description: 问题描述 （新增时为必填，更新时为选填）</i></li>" +
                    "<li><i>选填：maintainComment: 维修描述</i></li>" +
                    "<li><i>选填：maintainCost: 维修费用</i></li>" +
                    "<li><i>必填：status: 状态， maintaining - 维修中 / maintained -- 维修完成 /  discard - 丢弃, 损坏</i></li>" +
                    "<li><i>选填: location: 设备维修位置" +
                    "<li><i>选填: endDate：维修结束时间 格式：yyyy-MM-dd HH:mm:SS 例如 2018-06-19 07:54:01</i></li>" +
                    "</ul>")
    @PutMapping("/update")
    @RequiresPermissions("api:equipment:edit")
    public ResponseJson doUpdate(@RequestBody EquipmentMaintainEntity equipmentMaintain) {
        if(equipmentMaintain.getId() == null)
        {
            throw new RRException("维修ID不能为空!");
        }
        if(equipmentMaintain.getStatus() == null  || !Constant.EquipmentMaintainStatus.verify(equipmentMaintain.getStatus()))
        {
            throw new RRException("Status不能为空或值不正确!");
        }
        ValidatorUtils.validateEntity(equipmentMaintain, UpdateGroup.class);
        equipmentMaintainService.update(equipmentMaintain);
        return ResponseJson.success();
    }

    @ApiOperation(value = "POST -- 保存设备维修信息",
            notes = " REST 方式请求" +
                    "Request Body: <ul>" +
                    "<li><i>必填：code: 设备编码</i></li>" +
                    "<li><i>必填：description: 问题描述</i></li>" +
                    "<li><i>可选：location: 设备维修位置</i></li>" +
                    "</ul>")
    @PostMapping("/save")
    @RequiresPermissions("api:equipment:edit")
    public ResponseJson doSave(@RequestBody EquipmentMaintainEntity equipmentMaintain) {
        ValidatorUtils.validateEntity(equipmentMaintain, AddGroup.class);
        equipmentMaintain.setStartDate(new Date());
        equipmentMaintainService.save(equipmentMaintain);
        return ResponseJson.success();
    }

    @ApiOperation(value = "POST -- 批量删除设备维修记录",
            notes = " REST 方式请求" +
                    "Request Body: <ul>")
    @PostMapping("/delete")
    @RequiresPermissions("api:equipment:edit")
    public ResponseJson doDetele(@RequestBody Long[] maintainIds) {
        equipmentMaintainService.deleteBatch(maintainIds);
        return ResponseJson.success();
    }
}
